﻿using Iworld.Client.Framework.AuthorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 修改密码视图模型
    /// </summary>
    public class EditPasswordViewModel : SubPageFrameViewModelBase
    {

        string oldPassword;
        string newPassword;
        string newPasswordCheck;
        /// <summary>
        /// 旧密码
        /// </summary>
        public string OldPassword
        {
            get
            {
                return oldPassword;
            }
            set
            {
                if(oldPassword != value)
                {
                    oldPassword = value;
                    this.OnPropertyChanged("OldPassword");
                }
            }
        }

        /// <summary>
        /// 新密码
        /// </summary>
        public string NewPassword
        {
            get
            {
                return newPassword;
            }
            set
            {
                if (newPassword != value)
                {
                    newPassword = value;
                    this.OnPropertyChanged("NewPassword");
                }
            }
        }

        /// <summary>
        /// 确认新密码
        /// </summary>
        public string NewPasswordCheck
        {
            get
            {
                return newPasswordCheck;
            }
            set
            {
                if (newPasswordCheck != value)
                {
                    newPasswordCheck = value;
                    this.OnPropertyChanged("NewPasswordCheck");
                }
            }
        }

        /// <summary>
        /// 提交命令
        /// </summary>
        public UniversalCommand SubmitCommand { set; get; }

        public EditPasswordViewModel()
            : base(PageCategory.用户信息.ToString(), Page.修改密码.ToString())
        {
            this.SubmitCommand = new UniversalCommand((param) =>
            {
                if (this.IsBusy) return;
                if (NewPasswordCheck != NewPassword)
                {
                    this.ShowMsg("两次输入的新密码不一致");
                    return;
                }
                AuthorServiceClient asc = new AuthorServiceClient();
                asc.EditPasswordCompleted += this.WrapEventHandler<EditPasswordCompletedEventArgs>(EditPasswordCompleted);
                asc.EditPasswordAsync(new EditAuthorPasswordImport() { 
                 NewPassword = this.NewPassword, OldPassword=this.OldPassword, Token=DataManager.GetValue<string>(DataKey.Iworld_Token)
                });
                this.IsBusy = true;
            });
        }

        void EditPasswordCompleted(object sender, EditPasswordCompletedEventArgs e)
        {
            this.IsBusy = false;
            if(e.Result.Success)
            {
                this.ShowMsg("修改密码成功");
            }
            else
            {
                this.ShowError(e.Result.Error);
            }
        }


    }
}
